﻿using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using Microsoft.CSharp;

namespace Wicresoft.EKA.NRemedy.CodeGenerate
{
    public class CSharpModelGenerator
    {
        private CodeGeneratorOptions _options;
        private ARSchema asrsch;


        public CSharpModelGenerator(ARLoginContext context)
        {
            asrsch = new ARSchema(context);
            _options = new CodeGeneratorOptions();
            _options.BlankLinesBetweenMembers = false;
            _options.IndentString = "\t";
        }

        public CodeCompileUnit GenerateClassByFormName(string namespacem,string FormName,List<uint> ids, CSharpModelGeneratorDefaultFactory csmgdf)
        {
            CodeCompileUnit ccu = new CodeCompileUnit();
            CSharpModelGenerateCode cmgc = new CSharpModelGenerateCode();
            ccu = cmgc.GenerateClassByFormName(asrsch,namespacem, FormName,ids, csmgdf);
            return ccu;
        }

        public string GenerateCodeByCodeCompileUnit(CodeCompileUnit ccu)
        {
            CSharpCodeProvider provider = new CSharpCodeProvider();
            StringWriter sw = new StringWriter();
            string code = string.Empty;
            try
            {
                provider.GenerateCodeFromCompileUnit(ccu, sw, _options);
                sw.Flush();
                code = sw.GetStringBuilder().ToString();
                return code;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                sw.Close();
            }
        }

      
    }
}
